<?php
header("content-type:text/html;charset=utf-8");
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--开始--******************************************";
	echo "<hr>";
}
$row = $dir = $html = $php = $js = $css = $png = $jpg = $txt = $other = 0;
//读取所有
function getdir($path){
	global $row,$dir,$html,$php,$js,$css,$png,$jpg,$txt,$other;
	$arr=scandir($path);
	foreach($arr as $v){
		if($v!="."&&$v!=".."){
			if(is_dir($path."/".$v)){
				$row++;
				$dir++;
				echo $row."、目录：".$path."/".$v."<span style='color: sandybrown;'>==》》文件夹</span>";
				echo "<br>";
				getdir($path."/".$v);
			}else{
				$row++;
				$ext = substr($v, (strrpos($v, ".") + 1));
				if ($ext == 'html') {
					$html++;
					echo $row."、文件：" . $path . "/" . $v."<span style='color: greenyellow;'>==》》HTML文件</span>";
				} elseif ($ext == 'php') {
					$php++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: blue;'>==》》PHP文件</span>";
				} elseif ($ext == 'js') {
					$js++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: blueviolet;'>==》》JS文件</span>";
				} elseif ($ext == 'css') {
					$css++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: coral;'>==》》css文件</span>";
				} elseif ($ext == 'png') {
					$png++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: #00BFFF;'>==》》png图片</span>";
				} elseif ($ext == 'jpg') { 
					$jpg++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: #0066B3;'>==》》JPG图片</span>";
				} elseif ($ext == 'txt') {
					$txt++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: lightslategrey;'>==》》TXT文件</span>";
				} else {
					$other++;
					echo $row."、文件：" . $path . "/" . $v . "<span style='color: red;'>==》》其他文件</span>";
				}
				echo "<br>";
			}
		}
	}		
}
$dirname = $_POST['webname'];
if (!empty($_POST['webname'])) {
	echo $dirname;
	echo "<hr>";
	getdir($dirname);

	echo "<hr><hr>";
	echo "递归".$dirname . "</br>";
	echo "本次递归统计：递归文件夹" . $dir . "个！" ;
	echo "<br>";
	echo "遍历的文件总计:" . ($html+$php+$js+$css+$png+$jpg+$txt+$other) . "个!";
	echo "<br>";
	echo "其中：";
	echo "<br>";
	echo "遍历的HTML文件" . $html . "个!";
	echo "<br>";
	echo "遍历的PHP文件" . $php . "个!";
	echo "<br>";
	echo "遍历JS文件" . $js . "个!";
	echo "<br>";
	echo "遍历CSS文件" . $css . "个!";
	echo "<br>";
	echo "遍历png图片文件" . $png . "个!";
	echo "<br>";
	echo "遍历jpg图片文件" . $jpg . "个!";
	echo "<br>";
	echo "遍历txt文件" . $txt . "个!";
	echo "<br>";
	echo "遍历的其他文件" . $other . "个！<br>";
	echo "<hr>";
}
if (!empty($_POST['webname'])) {
	echo "<hr>";
	echo "****************************************--结束--******************************************";
	echo "<hr>";
}
?>
<html>
	<head>
		<meta charset="utf-8">
		<title>递归遍历</title>
	</head>
	<body>
		<form action="#" method="post">
			<br />
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入要递归的文件夹：
			<input type="text" name="webname" id="webname" value="<?php echo $dirname;?>" /> <br />
			<input style="margin-left: 300px;margin-top: 12px;" type="submit" value="开始递归"/>
		</form>
		<br />
		<br />
		<h3>注：输入本程序文件的相对目标文件夹</h3>
		<br />
		<br />
	</body>
</html>